import { CALL_API } from 'redux-api-middleware'
import { extend } from 'lodash/fp'
import cookie from 'react-cookie'
import config from 'config'

// Constants
export const FETCHFORUMTYPE_REQUEST = 'FETCHFORUMTYPE_REQUEST'
export const FETCHFORUMTYPE_SUCCESS = 'FETCHFORUMTYPE_SUCCESS'
export const FETCHFORUMTYPE_FAILURE = 'FETCHFORUMTYPE_FAILURE'

export const REGION_CHANGE = 'REGION_CHANGE'

export const FETCHFORUMLIST_REQUEST = 'FETCHFORUMLIST_REQUEST'
export const FETCHFORUMLIST_SUCCESS = 'FETCHFORUMLIST_SUCCESS'
export const FETCHFORUMLIST_FAILURE = 'FETCHFORUMLIST_FAILURE'

// Actions
export function fetchForumType () {
  return {
    [CALL_API]: {
      endpoint: config.api_root_v1 + '/article.php?c=article',
      method: 'POST',
      body: JSON.stringify({
        "jsonrpc": "2.0",
        "method": "classification",
        "params": [],
        "id": 1
      }),
      types: [
        FETCHFORUMTYPE_REQUEST,
        {
          type: FETCHFORUMTYPE_SUCCESS,
          payload: (action, state, res) => {
            return res.json()
          }
        },
        FETCHFORUMTYPE_FAILURE
      ]
    }
  }
}

export function changeRegion (regionId) {
  return {
    type: REGION_CHANGE,
    regionId
  }
}

export function fetchForumList (cid = '1', page = '1') {
  return {
    [CALL_API]: {
      endpoint: config.api_root_v1 + '/article.php?c=article',
      method: 'POST',
      body: JSON.stringify({
        "jsonrpc": "2.0",
        "method": "index",
        "params": [{
          "c_id": cid,
          "page": page
        }],
        "id": 1
      }),
      types: [
        FETCHFORUMLIST_REQUEST,
        {
          type: FETCHFORUMLIST_SUCCESS,
          payload: (action, state, res) => {
            return res.json()
          }
        },
        FETCHFORUMLIST_FAILURE
      ]
    }
  }
}

export const actions = {
  fetchForumType,
  fetchForumList,
  changeRegion
}

// Action Handlers
const ACTION_HANDLERS = {
  [REGION_CHANGE]: (state = 1, action) => {
    return ({ ...state, attractionsRegionId: action.regionId })
  },
  [FETCHFORUMTYPE_REQUEST]: (state, action) => {
    return ({ ...state, forumType: { isFetching: true, error: {}, items: {} } })
  },
  [FETCHFORUMTYPE_SUCCESS]: (state, action) => {
    return ({ ...state, forumType: { isFetching: false, error: {}, items: action.payload } })
  },
  [FETCHFORUMTYPE_FAILURE]: (state, action) => {
    return ({ ...state, forumType: { isFetching: false, error: {}, items: {} } })
  },
  [FETCHFORUMLIST_REQUEST]: (state, action) => {
    return ({ ...state, forumList: { isFetching: true, error: {}, items: {} } })
  },
  [FETCHFORUMLIST_SUCCESS]: (state, action) => {
    return ({ ...state, forumList: { isFetching: false, error: {}, items: action.payload, lastUpdate: Date.now() } })
  },
  [FETCHFORUMLIST_FAILURE]: (state, action) => {
    return ({ ...state, forumList: { isFetching: false, error: {}, items: {} } })
  }
}

// Reducers
const initialState = { forumType: { isFetching: false, error: {}, items: [] }, forumList: { isFetching: false, error: {}, items: [] } }
export default function forumHomeReducer (state = initialState, action) {
  const handler = ACTION_HANDLERS[action.type]
  return handler ? handler(state, action) : state
}
